﻿using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// Summary description for NumberToMenoy
/// </summary> 

public class NumberToMoney
{
    private int Fra, Hun, Tho, Mil, Bil;//.xx,100;1000;1000,000;1000,000,000
    private String[] Ones = new String[] { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
    private String[] Teens = new String[] { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
    private String[] Tys = new String[] { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };

    /**/
    /// <summary>
    /// divide every 3 dig. xx,xxx,xxx
    /// </summary>
    /// <param name="strn1"></param>
    private void Divide(string strNumber)
    {
        if (strNumber.IndexOf(".") > -1)
        {
            string strFra = strNumber.Substring(strNumber.IndexOf(".") + 1);//just 2 dig, for cents
            if (strFra.Length == 1)
                strFra += "0";
            else if (strFra.Length >= 2)
                strFra = strFra.Substring(0, 2);
            else
                strFra = "0";
            Fra = Int32.Parse(strFra);
            strNumber = strNumber.Substring(0, strNumber.IndexOf("."));//get left
        }
        int len = strNumber.Length;
        switch (len)
        {
            case 1:
                goto case 3;
            case 2:
                goto case 3;
            case 3:
                Hun = Int32.Parse(strNumber);
                break;
            case 4:
                goto case 6;
            case 5:
                goto case 6;
            case 6:
                Hun = Int32.Parse(strNumber.Substring(len - 3, 3));
                Tho = Int32.Parse(strNumber.Substring(0, len - 3));
                break;
            case 7:
                goto case 9;
            case 8:
                goto case 9;
            case 9:
                Hun = Int32.Parse(strNumber.Substring(len - 3, 3));
                Tho = Int32.Parse(strNumber.Substring(len - 6, 3));
                Mil = Int32.Parse(strNumber.Substring(0, len - 6));
                break;
            case 10:
                goto case 12;
            case 11:
                goto case 12;
            case 12:
                Hun = Int32.Parse(strNumber.Substring(len - 3, 3));
                Tho = Int32.Parse(strNumber.Substring(len - 6, 3));
                Mil = Int32.Parse(strNumber.Substring(len - 9, 3));
                Bil = Int32.Parse(strNumber.Substring(0, len - 9));
                break;
        }
    }



    /**/
    /// <summary>
    ///  number less than 1000
    /// </summary>
    /// <param name="number"></param>
    /// <returns></returns>
    private string SplitDigits(int number)
    {
        string strH = "", strT = "";

        int hun1 = 0, ten1 = 0, one1 = 0;//set 0 hur 0 tens and 0

        hun1 = number / 100;//get how much hundreds *******
        ten1 = (number % 100) / 10;// 
        one1 = (number % 100) % 10;

        if (hun1 > 0)//[1-9][00-99]
        {
            strH = Ones[hun1] + " Hundred ";
        }
        else if (hun1 == 0)//<100
        {
            strH = Ones[hun1];//not a hundred, then use " ".Ones[0]=" "  
        }
        if (ten1 > 1)//[2-9][0-9]
        {
            strT = Tys[ten1] + " " + Ones[one1];
        }
        else if (ten1 == 1)//11,12 no need one1s
        {
            strT = Teens[one1];//***** use number
        }
        else //ten1=0
        {
            strT = Ones[one1];
        }
        return strH + strT;

    }
    public string NumberToCurrency(decimal currency)
    {
        Divide(currency.ToString());//divide first

        string strHun = "", strTho = "", strMil = "", strBil = "", strFra = "";
        if (Fra > 0)
        {
            strFra = " and Cents " + SplitDigits(Fra) + "";
        }
        if (Hun > 0)
        {
            strHun = SplitDigits(Hun);// +" Dollars ";
        }
        if (Tho > 0)
        {
            //strTho = SplitDigits(Tho) + " Thousand and ";
            strTho = SplitDigits(Tho) + " Thousand ";
        }
        if (Mil > 0)
        {
            strMil = SplitDigits(Mil) + " Million ";
            //strMil = SplitDigits(Mil) + " Million and ";
        }
        if (Bil > 0)
        {
            strBil = SplitDigits(Bil) + " Billion ";
            //strBil = SplitDigits(Bil) + " Billion and ";
        }

        return (strBil + strMil + strTho + strHun + strFra);

    }
}
